package com.kyle.java.queue;

import java.util.Arrays;
import java.util.Objects;

public class ArrayQueue {
    private int[] arr;
    private int maxsize = 10; //队列最大容纳元素10
    private int front = -1; //队列为空时，队头指针 置为 -1
    private int rear = -1; //队列为空时，队尾指针 置为 -1

    //队列初始化
    public ArrayQueue() {
        arr = new int[maxsize];
    }
    //元素入队
    public boolean add(int elem) {
        if (arr == null) {
            new ArrayQueue(); //队列初始化
        }

        if ((rear == maxsize - 1 && front == 0) || rear == front - 1 ) {
            System.out.println("队列满，添加失败");
            return false;
        } else {
            if(rear == maxsize-1 && front!=0) { //如果队列未满，但是数据存储已到数组尾部,则循环存储数据到0
                if(front ==maxsize -1 ){
                    front=0;
                }
                arr[0] = elem;
                rear = 0;
            }else{
                arr[rear + 1] = elem;//队尾未排到0，
                rear++;
            }

            if(front == -1){ //如果添加的是第一个元素，则将 队列头序号置为1
                front = 0;
            }


            return true;
        }
    }

    public boolean pop() {
        if (front < 0 || front > rear) {
            System.out.println("队列已空");
            return false;
        } else {
            System.out.println(arr[front]);
            if(front!= maxsize-1){
            front++;
            }
                     return true;
        }
    }

    public int[] getArr() {
        return arr;
    }
    public void showArray(){
        for(int i = 0 ; i < maxsize ; i++){
            System.out.println(arr[i]);
        }
    }
}
